{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from typing import List\n",
    "import random\n",
    "import sys\n",
    "import import_ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transpositionEncrypt import encrypt_message\n",
    "from transpositionDecrypt import decrypt_message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_random_message() -> str:\n",
    "    \"\"\"生成随机测试消息\"\"\"\n",
    "    base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n",
    "    message = list(base * random.randint(1, 4))\n",
    "    random.shuffle(message)\n",
    "    return ''.join(message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_encryption_roundtrip(message: str, max_key_size: int) -> bool:\n",
    "    \"\"\"测试单个消息的加密-解密往返\"\"\"\n",
    "    for key in range(1, max_key_size + 1):\n",
    "        encrypted = encrypt_message(key, message)\n",
    "        decrypted = decrypt_message(key, encrypted)\n",
    "\n",
    "        num_columns = math.ceil(len(message) / key)\n",
    "        num_rows = key\n",
    "        num_full_rows = len(message) % key or num_rows\n",
    "        row_lengths = [num_columns] * num_full_rows + [num_columns - 1] * (num_rows - num_full_rows)\n",
    "\n",
    "        start_idx = 0\n",
    "        for i, length in enumerate(row_lengths):\n",
    "            end_idx = start_idx + length\n",
    "            print(f\"行{key:2d}-{i+1:2d}-{start_idx:2d}:{encrypted[start_idx:end_idx]}\")\n",
    "            start_idx = end_idx\n",
    "        print(\"\")\n",
    "        \n",
    "        \n",
    "        if message != decrypted:\n",
    "            print(f\"❌ 测试失败 - 密钥: {key}\")\n",
    "            print(f\"原始消息: {message[:50]}...\")\n",
    "            print(f\"解密结果: {decrypted[:50]}...\")\n",
    "            return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_test_suite(num_tests: int = 5) -> None:\n",
    "    \"\"\"运行完整的测试套件\"\"\"\n",
    "    random.seed(42)  # 固定种子确保可重复性\n",
    "    \n",
    "    for test_num in range(1, num_tests + 1):\n",
    "        message = generate_random_message()\n",
    "        max_key = max(1, len(message) // 2 - 1)  # 确保密钥至少为1\n",
    "        \n",
    "        print(f\"🔍 测试 #{test_num:2d}: {message[:50]}... (长度: {len(message)}, 最大密钥: {max_key})\")\n",
    "        \n",
    "        if not test_encryption_roundtrip(message, max_key):\n",
    "            sys.exit(1)\n",
    "    \n",
    "    print(\"✅ 置换密码测试全部通过\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "run_test_suite()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
